from PySide6.QtCore import QFile
from PySide6.QtUiTools import QUiLoader

from src.main.server.controller.vol_info_controller import VolInfoController

class EditVolInfoWindow:
    def __init__(self, main_window, data=None):
        self.original_data = None
        self.data = None
        if data:
            self.data = data
        self._load_ui()
        self.main_window = main_window

    def _load_ui(self):
        path = "../../../qt_ui/edit_vol_info.ui"
        file = QFile(path)
        try:
            loader = QUiLoader()
            self.window = loader.load(file)
            # 性别按钮组
            self.window.buttonGroup.buttonClicked.connect(self.handle_gender_selection)

            self.window.pushButton_2.clicked.connect(self.close_window) #关闭

        finally:
            file.close()

        if self.window:
            self.window.show()  # 显示独立窗口
        else:
            print("加载 UI 失败，请检查文件路径或内容")

        if self.data:
            """有数据则回显数据"""
            self.get_info()

    def handle_gender_selection(self):
        """按钮组的动态获取"""
        selected_button = self.window.buttonGroup.checkedButton()
        if selected_button:
            self.gender = selected_button.text()
        else:
            self.gender = None

    def get_info(self):
        """查询回显"""
        self.handle_gender_selection()
        self.window.lineEdit.setText(self.data[0][0])         #姓名
        self.original_data = self.data[0][0]  #原来数据，方便修改
        self.window.lineEdit_5.setText(self.data[0][1])       #密码
        self.window.lineEdit_2.setText(str(self.data[0][2]))  #年龄

        gender_value = str(self.data[0][3])
        if gender_value == '1':
            gender_value = "男"
        else:
            gender_value = "女"

        print(gender_value)
        # 遍历 buttonGroup 中的所有 QRadioButton
        for button in self.window.buttonGroup.buttons():
            if button.text() == gender_value:
                button.setChecked(True)
                break

        self.window.lineEdit_3.setText(self.data[0][4])       #联系方式
        self.window.lineEdit_4.setText(str(self.data[0][5]))  #积分

        self.window.pushButton.clicked.connect(self.submit)

    def submit(self):
        """更新数据"""
        vol_name = self.window.lineEdit.text()
        password = self.window.lineEdit_5.text()
        age = self.window.lineEdit_2.text()
        gender = self.gender
        if gender == "男":
            gender = 1
        else:
            gender = 0
        pho = self.window.lineEdit_3.text()
        score = self.window.lineEdit_4.text()
        VolInfoController().update((vol_name, password, age, gender, pho, score, self.original_data))
        print("推送成功")

        self.main_window.new_data()
        self.close_window()

    def close_window(self):
        """关闭当前窗口"""
        self.window.close()

def edit_vol_info_window(self, data = None):
    if data:
        self.edit_window = EditVolInfoWindow(self, data)
    else:
        self.edit_window = EditVolInfoWindow(self)